<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller\curd\service;

class BuildSearch
{
    public function build($data)
    {
        $html = '';
        foreach ($data as $_data) {
            if ( $_data['search'] == 1) {
                if ( in_array($_data['form_type'], ['input', 'textarea', 'number']) ) {
                    $html .= $this->createInput($_data).PHP_EOL;
                } elseif ( in_array($_data['form_type'], ['radio', 'checkbox', 'select', 'switch'])) {
                    $html .= $this->createSelect($_data).PHP_EOL;
                }
            }
        }
        $html = trim($html);
        $html = str_replace('###', '$', $html);
        return $html;
    }

    /**
     * 文本
     * @param $arr
     * @return string
     */
    protected function createInput($data)
    {
        $html=<<<php
                    <div class="layui-inline">
                        <label class="layui-form-label">{$data['name']}</label>
                        <div class="layui-input-inline">
                            <input type="text" name="{$data['field']}" placeholder="请输入关键字" value="{:input('{$data['field']}')}" autocomplete="off" class="layui-input" >
                        </div>
                    </div>
php;
        return $html;
    }

    /**
     * 下拉
     * @param $data
     * @return string
     */
    protected function createSelect($data)
    {
        $rule = $data['form_rule'];
        if ( empty($rule) ) {
            retError('选规则不能为空');
        }
        //$rule = explode(PHP_EOL, $rule);
        $rule = explode("\n", $rule);
        $option_html = '';
        foreach ($rule as $k => $v) {
            $rule[$k] = trim($v);
            if ( empty($rule[$k]) ) {
                retError('选规则不能为空');
            }
            $rule[$k] = explode('|', $rule[$k]);
            if ( count($rule[$k]) != 2 ) {
                retError('选规则格式错误');
            }
            $option_html .= <<<php
                                <option value="{$rule[$k][0]}" {if condition="input('{$data['field']}')!='' and (input('{$data['field']}') eq '{$rule[$k][0]}')" } selected="selected" {/if}>{$rule[$k][1]}</option>
php;
            $option_html .= PHP_EOL;
        }
        $option_html = trim($option_html);

        $html=<<<php
                    <div class="layui-inline">
                        <label class="layui-form-label">{$data['name']}</label>
                        <div class="layui-input-inline">
                            <select name="{$data['field']}">
                                <option value="">--不限--</option>
                                {$option_html}
                            </select>
                        </div>
                    </div>
php;
        return $html;
    }
}
